package com.github.WhiteMagic2014.gptApi.Chat.pojo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.github.WhiteMagic2014.util.GptImageUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * For more detailed information on function calling, please refer to https://platform.openai.com/docs/guides/gpt/function-calling
 *
 * @Description: CreateChatCompletionRequest message
 * @author: magic chen
 * @date: 2023/3/2 10:41
 **/
public class ChatMessage implements Serializable {

    private static final long serialVersionUID = 1L;


    /**
     * The role of the messages author. One of system, user, assistant, or function.
     */
    private String role;

    /**
     * An optional name for the participant. Provides the model information to differentiate between participants of the same role.
     */
    private String name;

    /**
     * The contents of the message. content is required for all messages except assistant messages with function calls.
     */
    @JSONField(serialzeFeatures = SerializerFeature.WriteMapNullValue)
    private Object content;


    /**
     * Tool call that this message is responding to.
     */
    private String tool_call_id;


    /**
     * The tool calls generated by the model, such as function calls.
     */
    private JSONArray tool_calls;

    public static ChatMessage systemMessage(String content) {
        return new ChatMessage("system", content);
    }

    public static ChatMessage systemMessage(String name, String content) {
        return new ChatMessage("system", name, content);
    }


    public static ChatMessage userMessage(String content) {
        return new ChatMessage("user", content);
    }


    public static ChatMessage userMessage(String name, String content) {
        return new ChatMessage("user", name, content);
    }

    public static ChatMessage userMessageWithImageUrl(String content, List<String> urls) {
        ChatMessage tmp = new ChatMessage();
        tmp.role = "user";

        List<JSONObject> list = new ArrayList<>();
        JSONObject text = new JSONObject();
        text.put("type", "text");
        text.put("text", content);
        list.add(text);
        for (String url : urls) {
            JSONObject u = new JSONObject();
            u.put("type", "image_url");
            u.put("image_url", url);
            list.add(u);
        }
        tmp.content = list;
        return tmp;
    }


    public static ChatMessage userMessageWithImageUrl(String content, String name, List<String> urls) {
        ChatMessage tmp = new ChatMessage();
        tmp.role = "user";
        tmp.name = name;

        List<JSONObject> list = new ArrayList<>();
        JSONObject text = new JSONObject();
        text.put("type", "text");
        text.put("text", content);
        list.add(text);
        for (String url : urls) {
            JSONObject u = new JSONObject();
            u.put("type", "image_url");
            u.put("image_url", url);
            list.add(u);
        }
        tmp.content = list;
        return tmp;
    }


    public static ChatMessage userMessageWithImageFilePath(String content, List<String> paths) {
        ChatMessage tmp = new ChatMessage();
        tmp.role = "user";
        List<JSONObject> list = new ArrayList<>();
        JSONObject text = new JSONObject();
        text.put("type", "text");
        text.put("text", content);
        list.add(text);
        for (String path : paths) {
            JSONObject u = new JSONObject();
            u.put("type", "image_url");
            JSONObject b64 = new JSONObject();
            b64.put("url", GptImageUtil.imageToBase64(path));
            u.put("image_url", b64);
            list.add(u);
        }
        tmp.content = list;
        return tmp;
    }

    public static ChatMessage userMessageWithImageFilePath(String content, String name, List<String> paths) {
        ChatMessage tmp = new ChatMessage();
        tmp.role = "user";
        tmp.name = name;
        List<JSONObject> list = new ArrayList<>();
        JSONObject text = new JSONObject();
        text.put("type", "text");
        text.put("text", content);
        list.add(text);
        for (String path : paths) {
            JSONObject u = new JSONObject();
            u.put("type", "image_url");
            JSONObject b64 = new JSONObject();
            b64.put("url", GptImageUtil.imageToBase64(path));
            u.put("image_url", b64);
            list.add(u);
        }
        tmp.content = list;
        return tmp;
    }


    public static ChatMessage assistantMessage(String content) {
        return new ChatMessage("assistant", content);
    }

    public static ChatMessage assistantMessage(String name, String content) {
        return new ChatMessage("assistant", name, content);
    }

    public static ChatMessage toolMessage(String toolCallId, String content) {
        ChatMessage tmp = new ChatMessage("tool", content);
        tmp.tool_call_id = toolCallId;
        return tmp;
    }

    public ChatMessage() {
    }

    public ChatMessage(String role, String content) {
        this.role = role;
        this.content = content;
    }

    public ChatMessage(String role, String name, Object content) {
        this.role = role;
        this.name = name;
        this.content = content;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Object getContent() {
        return content;
    }

    public void setContent(Object content) {
        this.content = content;
    }

    public String getTool_call_id() {
        return tool_call_id;
    }

    public void setTool_call_id(String tool_call_id) {
        this.tool_call_id = tool_call_id;
    }

    public JSONArray getTool_calls() {
        return tool_calls;
    }

    public void setTool_calls(JSONArray tool_calls) {
        this.tool_calls = tool_calls;
    }

    @Override
    public String toString() {
        return "ChatMessage{" +
                "role='" + role + '\'' +
                ", name='" + name + '\'' +
                ", content=" + content +
                ", tool_call_id='" + tool_call_id + '\'' +
                ", tool_calls=" + tool_calls +
                '}';
    }
}
